#include <stdio.h>
#include "MenuElementList.h"

MenuElementList::MenuElementList()
{
	head = NULL;
}

MenuElementList::MenuElementList(MenuElement * headMenuElement)
{
	head = new List<MenuElement>(headMenuElement, NULL);
}

MenuElementList::~MenuElementList()
{
	printf("Deleting MenuElement list\n");
	List<MenuElement> * search = head;
	while(search)
	{
		search = search->Remove();
	}
	printf("Done deleting MenuElement list list\n");
	head = NULL;
}
void MenuElementList::Add(MenuElement * payload)
{
	head = new List<MenuElement>(payload, head);
}
void MenuElementList::Draw(Vector2D position)
{
	List<MenuElement> * search = head;
	while(search)
	{
		if(search->payload)
			search->payload->Draw(position);
		search = search->next;
	}	
}
bool MenuElementList::HandleUserInput(unsigned char key, int modifier, bool isDepressed)
{
	List<MenuElement> * search = head;
	while(search)
	{
		if(search->payload)
		{
			printf("Should be handling user input\n");
			if(search->payload->HandleUserInput(key,modifier,isDepressed))
				return true;
		}
		search = search->next;
	}	
	return false;
}
int MenuElementList::Click(Vector2D parentPosition, Vector2D mousePosition)
{
	List<MenuElement> * search = head;
	int res = -1;
	int i = 0;
	while(search)
	{
		if(search->payload)
			if(search->payload->Click(parentPosition, mousePosition))
				res = i;
		i++;
		search = search->next;
	}	
	return res;
}
MenuElement * MenuElementList::GetMenuElement(int index)
{
	List<MenuElement> * search = head;
	int i = 0;
	while(search)
	{
		if(i == index)
			return search->payload;
		i++;
		search = search->next;
	}	
	return NULL;
}